<?php

class stToolBarController extends stcontroller_2_1 {

    public function __construct($config) {
        parent::__construct($config);
    }

    public function loadAdminCss() { // load more css custom here
        
    }

    public function loadAdminScript() { // load more js custom here
        
    }

    public function customAdminEnqueueScript() {
        parent::customAdminEnqueueScript();
    }

    public function loadCss() {
        //this is function import javascript
        parent::loadCss();
        echo '<link href="' . $this->config ['backendCss'] . 'toolbar/toolbar.css" rel="stylesheet" type="text/css" />' . "\n";
        echo '<link href="' . $this->config ['frontendCss'] . 'facebook/fb.css" rel="stylesheet" type="text/css" />' . "\n";
        echo '<link href="' . $this->config ['frontendCss'] . 'facebook/my.css" rel="stylesheet" type="text/css" />' . "\n";
    }

    public function loadScript() {
        //this is function import javascript
        parent::loadScript();
        wp_enqueue_script('jquery UI', $this->config ['backendScript'] . 'jquery-ui-1.8.7.custom.min.js', array('jquery'));
        wp_enqueue_script('tooltip', $this->config ['backendScript'] . 'jquery.ui.tooltip.js', array('jquery'));
        wp_enqueue_script('fbaction', $this->config ['frontendScript'] . 'toolbar/' . 'actions.js', array('jquery'));
        wp_enqueue_script('fbpage', $this->config ['frontendScript'] . 'toolbar/' . 'fbpage.js', array('jquery'));
        wp_enqueue_script('stToolBarCustom', $this->config ['frontendScript'] . 'stToolBarCustom.js', array('jquery'));
        wp_enqueue_script('blockUI', $this->config ['backendScript'] . 'jquery.blockUI.js', array('jquery'));
    }

    public function activationPlugin() {
    }

    public function deactivationPlugin() {
    }

    public function registerView() {
        $views = array(
            'showFbFanPage' => 'default_theme',
            'getFbFeed' => 'default_theme',
            'showYoutubePage' => 'default_theme',
            'showToolBar' => 'default_theme',
            'toolbarPreview' => 'preview_theme',
        );
        $this->setView($views);
    }

    public function index() {
        $youtubeUser_model = $this->loadmodel("stYoutubeToolboxuser");
        $facebookPage_model = $this->loadmodel("stFacebookPage");
        if ($_REQUEST['data']) {
            $data = $_REQUEST['data'];
            if(!empty($data['youtubeUserId'])){
                $userDetail = $youtubeUser_model->getUserDetail($data['youtubeUserId']);
                $userDetail = $userDetail[0];
                $data['youtubeUserName'] = $userDetail->user_name;
                $data['youtubePassword'] = $userDetail->user_password;
                $data['youtubeChannel'] = $userDetail->user_channel;
            }
            if(!empty($data['fbPageID'])){
                $pageDetail = $facebookPage_model->getFanPageDetail($data['fbPageID']);
                $pageDetail = $pageDetail[0];
                $data['toolFBPageID'] = $pageDetail->pageid;
            }
            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            }
        }
        $userList = $youtubeUser_model->getUserList();
        $pageList = $facebookPage_model->get_pagelist();
        $this->view->option = $this->option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Web Toolbar Settings';
        $this->view->userList = $userList;
        $this->view->pageList = $pageList;
    }

    public function showToolBar() {
        $option = $this->getOption('', $_REQUEST['user_id']);
        if($_REQUEST['external']&& !empty($_REQUEST['user_id'])){
            $url = empty($_REQUEST['url'])? home_url('/'):$_REQUEST['url'];
            $path = empty($_REQUEST['path'])? home_url('/'):$_REQUEST['path'];
            echo '|**|'.$this->config['pluginPath']."views/showFbFanPage.php?pid={$option['toolFBPageID']}&api_key={$this->config['facebook_api_key']}&url=".$url.'&path='.$path.'|**|';
        }
        $this->view->option = $option;
    }

    public function showFbFanPage() {
        $option = $this->getOption('', 1);
        $this->view->option = $option;
    }

    public function showYoutubePage() {
        $current_video_id = $_REQUEST['videoId'];
        $option = $this->getOption('', $_REQUEST['user_id']);

        $user->user_name = $option['youtubeUserName'];
        $user->user_password = $option['youtubePassword'];
        $user->user_channel = $option['youtubeChannel'];
        $user_name = basename($option['youtubeChannel']);
        
        require_once $this->config['pluginDir'] . 'stYoutubeToolboxclass.php';
        $stYoutubeToolbox = new stYoutubeToolboxclass($this->config, $option);
        
        if (empty($_SESSION['youtubeObject'])) {
            $youtubeObject = $stYoutubeToolbox->youtubeClientLogin($user);
            $_SESSION['youtubeObject'] = serialize($youtubeObject);
        } else {
            $youtubeObject = unserialize($_SESSION['youtubeObject']);
        }
        $videoUploads = $youtubeObject->getUserUploads($user_name);
        $videoFavourites = $youtubeObject->getUserFavorites($user_name);

        $this->view->option = $option;
        $this->view->videoUploads = $videoUploads;
        $this->view->youtubeObject = $youtubeObject;
        $this->view->videoFavourites = $videoFavourites;
        $this->view->current_video_id = $current_video_id;
    }

    public function getFbFeed() {
        require_once $this->config['pluginDir'] . 'libs/facebook/facebook.php';
        $appapikey = $this->config['facebook_api_key'];
        $appsecret = $this->config['facebook_secret'];
        //$appId = '108269745874300';

        $facebook = new Facebook($appapikey, $appsecret, true);

        $action = @$_REQUEST['type'];
        $pid = @$_REQUEST['pid']; //114225711941356
        $start = @$_REQUEST['start'];
        if (null == $start) {
            $start = 0;
        }
        $this->view->start = $start;
        if ($action == 'getPageName') {
            $pageDetails = $pageDetails = $facebook->api_client->pages_getInfo($pid, 'name,pic_square,page_url ', null, null);
            if (!is_array($pageDetails)) {
                die;
            }
            $pageDetails = current($pageDetails);
            echo @$pageDetails['name'];
            die;
        } else if ($action == 'getAll') {
            /**
             * Print all comments
             */
            $facebook->api_client->user = null; //$userId;
            $facebook->api_client->session_key = null;
            $facebook->api_client->expires = 0;

            $datas = $facebook->api_client->stream_get(0, $pid, null, null, $start + 30);
//            var_dump($datas);
            $posts = @$datas['posts'];
//            var_dump($posts);
            $profiles = @$datas['profiles'];
            $this->view->posts = $posts;
            $this->view->profiles = $profiles;
//            require_once 'getAll.php';
        } else if ($action == 'getOnlyPage') {
            /**
             * Print all comments
             */
            $facebook->api_client->user = null; //$userId;
            $facebook->api_client->session_key = null;
            $facebook->api_client->expires = 0;
            $datas = $facebook->api_client->stream_get(0, $pid, null, null, $start + 30);
            $posts = @$datas['posts'];

            if (is_array($posts)) {
                foreach ($posts as $index => $post) {
                    if ($post['actor_id'] != $pid) {
                        /**
                         * Remove
                         */
                        unset($posts[$index]);
                    }
                }

                $profiles = @$datas['profiles'];
            }
            $this->view->posts = $posts;
            $this->view->profiles = $profiles;
//            require_once 'getAll.php';
        } else if ($action == 'getOnlyFans') {
            /**
             * Print all comments
             */
            $facebook->api_client->user = null; //$userId;
            $facebook->api_client->session_key = null;
            $facebook->api_client->expires = 0;
            $datas = $facebook->api_client->stream_get(0, $pid, null, null, $start + 30);
            $posts = @$datas['posts'];

            if (is_array($posts)) {
                foreach ($posts as $index => $post) {
                    if ($post['actor_id'] == $pid) {
                        /**
                         * Remove
                         */
                        unset($posts[$index]);
                    }
                }

                $profiles = @$datas['profiles'];
            }
            $this->view->posts = $posts;
            $this->view->profiles = $profiles;
//            require_once 'getAll.php';
        }

        function getTime($time) {
            $minute = 60;
            $hour = 3600;
            $day = 24 * 3600;
            $week = 7 * $day;
            $month = 30 * $day;
            $year = 365 * $day;

            $time = time() - $time;
            if ($time < 0) {
                return "1 second ago";
            }

            if ($time >= $year) {
                $time = intval($time / $year);
                if ($time == 1) {
                    return "1 year ago";
                }
                return "$time years ago";
            }
            if ($time >= $month) {
                $time = intval($time / $month);
                if ($time == 1) {
                    return "1 month ago";
                }
                return "$time months ago";
            }
            if ($time >= $week) {
                $time = intval($time / $week);
                if ($time == 1) {
                    return "1 week ago";
                }
                return "$time weeks ago";
            }
            if ($time >= $day) {
                $time = intval($time / $day);
                if ($time == 1) {
                    return "1 day ago";
                }
                return "$time days ago";
            }
            if ($time >= $hour) {
                $time = intval($time / $hour);
                if ($time == 1) {
                    return "1 hour ago";
                }
                return "$time hours ago";
            }
            if ($time >= $minute) {
                $time = intval($time / $minute);
                if ($time == 1) {
                    return "1 minute ago";
                }
                return "$time minutes ago";
            }
            return "$time seconds ago";
        }


    }

    public function toolbarPreview(){
        $option = $_REQUEST['data'];
        $this->view->option = $option;
        if(!$option['toolIsShow'])
            die;
    }

}

?>